package org.example.hzit.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.example.hzit.OrderVO.OrderVo;
import org.example.hzit.client.userClient;
import org.example.hzit.entity.Order;
import org.example.hzit.entity.User;
import org.example.hzit.mapper.OrderMapper;
import org.example.hzit.repository.OrderRepository;
import org.example.hzit.service.IOrderService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.example.hzit.util.PageResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author gww
 * @since 2025-01-16
 */
@Service

public class OrderServiceImpl extends ServiceImpl<OrderMapper, Order> implements IOrderService {

    @Autowired
    private OrderRepository orderRepository;

    @Autowired
    private userClient c;


    @Override
    public PageResult<Order> findByPage(Integer page, Integer pageSize) {
        Page<Order> page1=this.page(new Page<>(page,pageSize));

        return new PageResult<>(page1.getRecords(),page1.getTotal());
    }

    @Override
    public PageResult<Order> search(Integer page, Integer pageSize, OrderVo vo) {
        QueryWrapper<Order> wrapper=new QueryWrapper<>();
        wrapper.like(StrUtil.isNotBlank(vo.getUserId()),"nameId",vo.getUserId());
        wrapper.eq(vo.getPayment() != null && vo.getPayment() != 0,"payment",vo.getPayment());
        Page<Order>orderPage = this.page(new Page<>(page, pageSize), wrapper);

        return new  PageResult<>(orderPage.getRecords(),orderPage.getTotal());
    }



    @Override
    public Order addOrder(Order order) {
        User user = order.getUser();
        // 调用用户微服务添加用户
        User addedUser = c.addUser(user);
        // 设置订单关联的用户为添加后的用户
        order.setUser(addedUser);
        // 保存订单
        return orderRepository.save(order);
    }




}
